var express=require('express');
var app=express();
var fs=require('fs');


//RESTful API delUser
app.get('/deleteUser',function(req,res)
{
    var delId=3;

    fs.readFile(__dirname+'\\'+'users.json','utf8',function(err,data)
    {
        if(err)
            console.errno(err);
        data=JSON.parse(data);
        delete data["user"+delId];
        console.log(data);
        res.end(JSON.stringify(data));
    });
});

//RESTful API listUsers
app.get('/listUsers',function(req,res)
{
    fs.readFile(__dirname+'\\'+'users.json','utf8',function(err,data)
    {
        console.log("listUsers",data);
        res.end(data);
    });
});

//RESTful API addUser
app.get('/addUser',function(req,res)
{
    var user={
        "user4":{
        "name":"ruby4",
        "pass":"1234",
        "profession":"teacher4",
        "id":4
        }
    };
    console.log('addUser:',user);
    fs.readFile(__dirname+'\\'+'users.json','utf8',function(err,data){
        data=JSON.parse(data);
        data['user4']=user['user4'];
        res.end(JSON.stringify(data));
    });
})

//RESTful API :id
app.get('/:id',function(req,res)
{
    fs.readFile(__dirname+'\\'+'users.json','utf8',function(err,data)
    {
        data=JSON.parse(data);
        var user=data["user"+req.params.id];
        console.log(user);
        res.end(JSON.stringify(user));
    });
});



var server=app.listen(8888,function()
{
    var host=server.address().address;
    var port=server.address().port;

    console.log("访问地址为 http://%s:%s", host, port);
})